/*
 * @(#)SaveXmlAction.java
 *
 *
 * ver 1.0 Oct 3, 2007 ggranum
 */
package org.testfire.agent.action;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.testfire.agent.AgentProperties;

// eof: SaveXmlAction.java

/**
 * Save an XML Testset to the local store.
 */
public class DeleteAction extends Action
{
    public DeleteAction()
    {
        super( false, new String[]{"userName"}, new String[]{"testset"} );
    }

    public String perform( Map<String, String> params, Map<String, String[]> paramValues )
    {
        String userName = params.get( "userName" );
        String[] sets = paramValues.get( "testset" );
        File userDir = new File( AgentProperties.getInstance().getSaveDirectory(), userName );
        File testSrcDir = new File( AgentProperties.getInstance().getTestSrcDirectory(), userName );
        Arrays.sort( sets );
        if ( Arrays.binarySearch( sets, "all" ) >= 0 )
        {
            userDir.delete();
        }
        else
        {
            for ( String s : sets )
            {
                s = s.replace( '.', '/');
                s = s.substring( 0, s.lastIndexOf( '/')) + ".";

                File f = new File( userDir, s + "xml");
                if ( f.exists() )
                {
                    f.delete();
                }
                f = new File( testSrcDir, s + "java");
                if ( f.exists() )
                {
                    f.delete();
                }
            }
        }
        return "";
    }

}